package com.qiaofang.collectmanage.analysis;

import com.alibaba.fastjson.JSONObject;
import com.qiaofang.collectmanage.domain.ElementRule;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.nodes.Element;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Arrays;
import java.util.List;

/**
 * Created with IDEA
 * 解析Html文本
 * <p>
 * 此类方法值只允许，新增，不允许修改 ，修改需要review
 *
 * @author:ZJ.J
 * @Date:2018/6/8
 * @Time:14:51
 */
public class HtmlAnalysisUtils {
    private static final Logger logger = LoggerFactory.getLogger(HtmlAnalysisUtils.class);
    /**
     * 多个方法用斜杠分隔符
     */
    /**
     * 参数分隔符
     */

    public static JSONObject analysisRule(Element element, List<ElementRule> listElements, JSONObject jsonObject) throws Exception {
        for (ElementRule elementRule : listElements) {
            String returnFieldValue = analysisField(element, elementRule, jsonObject);
            returnFieldValue = StringUtils.isBlank(returnFieldValue) ? null : returnFieldValue.toString().trim();
            jsonObject.put(elementRule.getFieldName(), returnFieldValue);
        }
        return jsonObject;
    }

    /**
     * 提供给爬虫
     *
     * @param element
     * @param listElements
     * @param jsonObject
     * @return
     */
    public static JSONObject mainAnalysisRule(Element element, List<ElementRule> listElements, JSONObject jsonObject, String debugField) {
        for (ElementRule elementRule : listElements) {
            String fieldName = elementRule.getFieldName();
            if (StringUtils.isNotBlank(debugField) && debugField.equals(fieldName)) {
                logger.info("debugName : {}", debugField);
            }
            String returnFieldValue = null;
            try {
                returnFieldValue = analysisField(element, elementRule, jsonObject);
                returnFieldValue = StringUtils.isBlank(returnFieldValue) ? "" : returnFieldValue.trim();
                if (StringUtils.isNotBlank(debugField)) {
                    logger.info("{} : {}", fieldName, returnFieldValue);
                }
            } catch (Exception e) {
                logger.error("error, {} : {} ", fieldName, returnFieldValue, e);
            }
            jsonObject.put(fieldName, returnFieldValue);
        }
        return jsonObject;
    }

    public static List<String> analysisRule(Element element, ElementRule elementRule, JSONObject jsonObject) throws Exception{
            String returnFieldValue = analysisField(element, elementRule, jsonObject);
            return Arrays.asList(returnFieldValue.split(";"));
    }

    private static String analysisField(Element element, ElementRule elementRule, JSONObject jsonObject) throws Exception {
        //1、定位元素
        Element lastElement = element;
        String returnFieldValue = "";
        if (element != null) {
            String returnElement = elementRule.getReturnElement();
            if (null != lastElement && StringUtils.isNotBlank(returnElement)) {
                String[] split1 = HtmlAnalysisConsts.SLASH.split(returnElement);
                for (int i = 0; i < split1.length; i++) {
                    String[] split11 = HtmlAnalysisConsts.PARAMETER_SEPARATOR.split(split1[i]);
                    lastElement = ReturnElementHandle.executeMethodByMethodName(lastElement, split11);
                }
            }
            //2、提取元素对应的值
            String elementValue = "";
            String returnElementValue = elementRule.getReturnElementValue();
            if (null != lastElement && StringUtils.isNotBlank(returnElementValue)) {
                String[] split2 = HtmlAnalysisConsts.PARAMETER_SEPARATOR.split(returnElementValue);
                elementValue = ReturnElementValueHandle.executeMethodByMethodName(lastElement, split2);
            }
            //3、对元素的值后续操作
            returnFieldValue = elementValue;
            String returnValue = elementRule.getReturnValue();
            if (StringUtils.isNotBlank(returnValue)) {
                String[] split3 = HtmlAnalysisConsts.SLASH.split(returnValue);
                if (StringUtils.isNotBlank(elementValue) || returnValue.contains(ReturnValueHandle.flow.MERGESTR.getMethodName())) {
                    for (int i = 0; i < split3.length; i++) {
                        String[] split33 = HtmlAnalysisConsts.PARAMETER_SEPARATOR.split(split3[i]);
                        if(StringUtils.isNotBlank(returnFieldValue)){
                            returnFieldValue = ReturnValueHandle.executeMethodByMethodName(returnFieldValue, jsonObject, split33);
                        }
                    }
                }
            }
        }
        return returnFieldValue;
    }
}
